package com.yd.mvc.pc.evaluateController;

import com.yd.app.service.pc.evaluateService.EvaluateService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Evaluate;
import com.yd.domain.commom.PageModel;
import com.yd.mvc.baseController.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评价
 * Created by sunxianhui on 2018/3/12.
 */
@RestController
@RequestMapping(value = "/api/evaluate")
public class EvaluateController extends BaseController {
    @Autowired
    EvaluateService evaluateService;

    /**
     * 添加
     * @param evaluate
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Evaluate addEvaluate(@RequestBody Evaluate evaluate) throws BusinessException {

        int count= evaluateService.addEvaluate(evaluate);
        if(count==1){
            return evaluate;
        }else {
            throw new BusinessException(500, "添加评价信息失败");
        }
    }

    /**
     * 修改
     * @param evaluate
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateEvaluate(@RequestBody Evaluate evaluate) {
        evaluateService.updateEvaluate(evaluate);
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteEvaluate(@PathVariable("id") String id) {
        evaluateService.deleteEvaluateByid(id);
    }


    /**
     * 根据条件分页查询
     * @param goodsId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{goodsId}/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageModel getEvaluateList(@PathVariable("goodsId") String goodsId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) throws BusinessException {
        PageModel pageModel = null;
        try {
            pageModel = evaluateService.getEvaluateList(goodsId, pageIndex, pageSize);
        } catch (Exception e) {
            throw new BusinessException(500, "获取评价信息失败");
        }
        return pageModel;
    }
}
